package main

import (
	"fmt"
	"net"
)

func main() {
	listen, err := net.Listen("tcp", "127.0.0.1:8080")
	if err != nil {
		panic("创建失败")
	}
	for{
		fmt.Println("server is waiting...")
		conn, err := listen.Accept()
		if err != nil {
			panic("连接失败")
		}
		data := make([]byte, 1024)
		n, _ := conn.Read(data)
		fmt.Println("data:", string(data[:n]))
		// 以纯文本方式返回解析
		// res := "HTTP/1.1 200 ok\r\ncontent-type:text/plain\r\n\r\n<h1>hello world</h1>"
		// 以html格式返回解析
		// res := "HTTP/1.1 200 ok\r\ncontent-type:text/html\r\n\r\n<h1>hello world</h1>"
		// 以json格式返回解析
		res := "HTTP/1.1 200 ok\r\ncontent-type:application/json\r\n\r\n{name:hankai,age:32}"
		conn.Write([]byte(res))
		conn.Close()
	}

}
